#!/usr/bin/perl
#index.cgi
use Data::Dumper;

require './diskman-lib.pl';
#`/usr/bin/perl /etc/rc.d/iscsicheck.pl`;
&ReadParse();
#$config{'mkfslock'} = "/var/nas/lock/mkfsd_new";
#if( -e $config{'mkfslock'}) {
#   &redirect("volstat.cgi");
#}

&header($text{'diskman_title'}, "", "index", 0, 1, undef, undef, "<meta http-equiv=refresh content=30>");

my (@rdinfo, $r);
my (@dinfo, $d);
my ($mkfs_lock);
$mkfs_lock = check_lock();

if ($mkfs_lock == 2) {
	@rdinfo = read_stale_raid_config();
	@dinfo = read_stale_disk_config();
}
else {
	@rdinfo = new_get_check_raid_info();
	@dinfo = new_get_diskinfo(\@rdinfo);
}
my @allraiddisks = &allraiddisks(@rdinfo);
my @diskraid = &raidofdisk(@rdinfo);
my $freedisk=0;
my ($validrd);
$validrd=0;
foreach $r (@rdinfo) {
	next if ($r->{'status'} eq 'none');
	$validrd++;
}
if ($validrd && new_has_free_disk(\@dinfo)) {
    #has volume and has free disk
    print "<form name=editform action=disk_man.cgi method=post onsubmit='return onsubmit_form(1,1,1,1);'>\n";
}
elsif ($validrd) {
    #has volume, but hasn't free disk, cannot create volume
    print "<form name=editform action=disk_man.cgi method=post onsubmit='return onsubmit_form(0,1,1,1);'>\n";
}
elsif (new_has_free_disk(\@dinfo)) {
    #has free disk, but hasn't volume, cannot detail, recovery, delete volume
    print "<form name=editform action=disk_man.cgi method=post onsubmit='return onsubmit_form(1,0,0,0);'>\n";
}
else {
    #has no volume or free disk
    print "<form name=editform action=disk_man.cgi method=post onsubmit='return onsubmit_form(0,0,0,0);'>\n";
}

print "<table width=100% class=mode2><tr>\n";

print "<td  valign=top><table  class=mode1 border=0 cellspacing=0
	cellpadding=1 width=100%>\n";

print "<tr class=title1> <th align=left> $text{'index_dname'}</th>",
	"<th align=left> $text{'index_dstatus'}</th>",
	"<th align=left> $text{'index_dsize'}</th>",
	"<th align=left> $text{'index_duse'}</th>",
	"<th align=left> $text{'index_dmodel'}</th> </tr>\n",

my ($dsizeM, $i, $j,$found);
$i = 0;
foreach $d (@dinfo) {
	next if ($d->{'id'} == 0);  # for promise adapters
        #print "*".$d->{'style'}."*\n";
	$i++;
	if ($i % 2 == 0) {
		$dismode = "class=mode2";
	}
	else {
		$dismode = "class=mode1";
	}

	if ( $d->{'status'} eq 'failed' ) {
		$disable= ' ';
		#$disable= 'disabled=true';
	}
	else {
		$disable= ' ';
		$found=1;
	}

	print "<tr $dismode>\n";
	print "<td nowarp $dismod width=20%>";
        #`echo $d->{'status'} > /stat`;
        $dvolname = get_volname($d->{'raidid'});
	if($mkfs_lock) { 
		$onclick=" ";
	} else {
		$onclick="onclick=\'checkstatus(\"$d->{'status'}\", \"$diskraid[$d->{'device'}]\")\'";
	}
	#print "<input type=radio name=disk value=$d->{'name'} ";
        print "<input type=radio name=disk value=$d->{'id'}";
	print " $disable $onclick >";
        if($d->{'id'} =~ /(\d+)/) {
           $tempdisk = "Disk".$1;
        }
	#print  $d->{'device'};
        print $tempdisk;
	print  "</td>\n";
	print "<td nowarp $dismod width=20%>",
		$d->{'status'}, "</td>\n";
	if ($d->{'status'} ne 'NOT-PRESENT') {
		$dsizeM=int($d->{'blocks'}/1024);
		print "<td nowarp $dismod width=20%>", $d->{'capacity'}, "</td>\n";
                $tempvol = $diskraid[$d->{'device'}]; #raid id of disk
		if($tempvol =~ /\d+/) {
	  		$tempvolname = $rdinfo[$tempvol]->{'volname'}; #raid name of disk
		}
		if (!($tempvol =~ /\d+/)) {
			if($d->{'spare'} eq '-'){
				print "<td nowarp $dismod width=20%>", $text{'unused'},
				"</td>\n";
				$freedisk = 1;
			}
			else{
				print "<td nowarp $dismod width=20%>",
				&text('used', 'SAPRE'), "</td>\n";
			}
		}
		else {
			print "<td nowarp $dismod width=20%>",
			&text('used', $tempvolname), "</td>\n";
		}
	}
	else {
		print "<td nowarp $dismod width=20%>",
			"<i>", $text{'NA'}, "</i>", "</td>\n";
		print "<td nowarp $dismod width=20%>",
			"<i>", $text{'NA'}, "</i>", "</td>\n";
	}
	if (defined($d->{'model'})) {
		print "<td nowarp $dismod width=20%>",
		"<i>", $text{'NA'}, "</i>", "</td>\n";
	}
	else {
		print "<td nowarp $dismod width=20%>",
		$d->{'style'}, "</td>\n";
	}
	print "</tr>\n";
}


print "</td></tr></table>\n";


#print (format disk) button
$disable='disabled=false';
print "<td valign=top width=60>";
print "<table class=mode1 border=0 cellspacing=8 cellpadding=1 width=6%>\n";
print "<tr><td align=center><input type=submit name=dformat value=\"$text{'rescan'}\" onclick='return SetEditValue_disk(this,$i)' class=button ></td></tr>\n";
#if($hwinfo{'hotswap_support'}==1) {
$hotswap_support = 0;
if($hotswap_support) {
print "<tr><td align=center><input type=submit name=dhtswap value=\"$text{'dhtswap'}\" onclick='return SetEditValue_disk(this,$i)' class=button $disable ></td></tr>\n";
} else {
print "<tr><td align=center><input type=hidden name=dhtswap value=\"$text{'dhtswap'}\" onclick='return SetEditValue_disk(this,$i)' class=button $disable ></td></tr>\n";
}
print "<tr><td align=center><input type=submit name=dspare value=\"$text{'sparedisk'}\" onclick='return SetEditValue_disk(this, $i)' class=button $disable></td></tr>\n";

print "</table></td>\n";
print "<tr> <td colspan=5> <hr> </td> \n";


#show the user volume information
print "<table width=100% class=mode2 cellspacing=0 cellpadding=0>\n";

print "<tr> <td  valign=top><table  class=mode1 border=0 cellspacing=0
cellpadding=1 width=100%>\n";
print "<tr class=title1> <th align=left> $text{'index_vname'}</th>",
	"<th align=left> $text{'index_vstatus'}</th>",
	"<th align=left> $text{'index_vrlevel'}</th>",
	"<th align=left> $text{'index_vpstatus'}</th>",
	"<th align=left> $text{'index_lvolcap'}</th>",
	"<th align=left> $text{'index_vcdisk'}</th> </tr>\n";
print "</tr>\n";

my ($has_usrvol);
$has_usrvol = 0;
$i = 0; $j = 0;
foreach $r (@rdinfo) {
	next if (($r->{'status'} eq 'none') || ($r->{'volname'} eq 'spare'));

	$i++;
	if ($i % 2 == 0) {
		$dismode = "class=mode2";
	}
	else {
		$dismode = "class=mode1";
	}


	print "<tr $dismode>\n";
        if ($r->{'name'} =~ /^u(\d+)/){
            #$temp_volname = "volume".($1+1);
            $temp_volname = $r->{'volname'};
        }
	if ($r->{'status'} eq 'ormal' && $r->{'status'} eq 'building') {
		#an 'unknown usrvol
                print "unknown raid";
		print "<td $dismod width=13%>";
		print "<input type=radio name=volume value=$r->{'volname'} >", $r->{'volname'}, "</td>\n";
		print "<td nowarp $dismod width=13%>", $text{'NA'}, "</td>\n"; #NA vstatus
		print "<td nowarp $dismod width=13%>", $text{'NA'}, "</td>\n"; #NA level
		print "<td nowarp $dismod width=17%>", $text{'NA'}, "</td>\n"; #NA pstatus
		print "<td nowarp $dismod width=17%>", $text{'NA'}, "</td>\n"; #NA integrity
		print "<td nowarp $dismod width=27%>", $text{'NA'}, "</td>\n"; #NA vcdisk
		print "</tr>\n";
		next;
	}

	#if the status is new or the raid is not integreted (has free disk), can edit this volume
	my ($can_recovery, $can_migrate);
	$can_recovery = 0;
	if(($r->{'status'} =~ /Critical/) && ($freedisk)){
		$can_recovery = 1;
	}
        #$can_recovery = new_can_recovery($r);
	$can_migrate = new_can_migrate($r);
	print "<td $dismod width=13%>";
	if ($mkfs_lock) {
		print "<input type=radio name=volume value=$r->{'volname'} >", $r->{'volname'}, "</td>\n";
	}
	else {
		print "<input type=radio name=volume value=$r->{'name'} onclick='SetRecoveryStatus($can_recovery, $can_migrate)' >", $r->{'volname'}, "</td>\n";
	}

	print "<td nowarp $dismod width=13%>";
	if ($r->{'status'} eq 'Online') {
		print $text{'normal'};
	}
        elsif($r->{'status'} eq 'Critical'){
                print $text{'degraded'};
        }
        elsif($r->{'status'} eq 'Rebuilding'){
                print $text{'rebuilding'};
        }
	elsif($r->{'status'} eq 'OFFLINE'){
		print $text{'offline'};
	}
        elsif($r->{'status'} eq 'INIT-PAUSED'){
		print $text{'building'};
        }
	elsif($r->{'status'} eq 'INITIALIZING'){
		print $text{'building'}
	}
	elsif(($r->{'status'} eq 'MIGRATING') || ($r->{'status'} eq 'MIGRATE-PAUSED')){
		print $text{'migrating'};
	}
	else {
		print $text{'NA'};
	}
	print "</td>\n";
	if($r->{'raidlevel'} ne 'Migrator'){
		print "<td nowarp $dismod width=13%>",
			$r->{'raidlevel'}, "</td>\n";
	}
	else{
		print "<td nowarp $dismod width=13%>$text{'migrating'}</td>\n";
	}
	print "<td nowarp $dismod width=17%>";
	if ($r->{'resync'}) {
		print $text{'resync'};
		if ($r->{'resync_delayed'}) {
			print "(", $text{'delayed'}, ")";
		}
		else {
			print "(", $r->{'percent'}, ")";
		}
	}
	elsif ($r->{'recovery'}) {
		print $text{'resync'},
			"(", $r->{'percent'}."%", ")";
	}
	else {
		print $text{'normal'};
	}
	print "</td>\n";

	print "<td nowarp $dismod width=17%>";
	if ($r->{'raid_disks'} != $r->{'working_disks'}) {
		#print $text{'unintegrated'};
		my $failed_disks;
		$failed_disks = $r->{'raid_disks'}-
					$r->{'working_disks'};
		if ($failed_disks > 1) {
			#print "(", $failed_disks,
				"&nbsp;",$text{'disks'}, "&nbsp;", $text{'failed'}, ")";
		}
		else {
			#print "(", $failed_disks,
				"&nbsp", $text{'disk'}, "&nbsp;", $text{'failed'}, ")";
		}
	}
	else {
		#print $text{'integrated'};
	}
	print $r->{'size'}." MB";
	print "</td>\n";

	print "<td nowarp $dismod width=27%>";		
	my $k = 0;
	my($rdev, @rdisknames, $rdev_status, @cdinfo);
	for ($k = 0; $k < @{$r->{'rdevs'}}; $k++) {
		$rdev = $r->{'rdevs'}->[$k];
		$rdev_status = $r->{'rdevs_role'}->[$k];
		if ($rdev_status eq 'good') {
			$cdinfo[$k] = &rdev2disk(\@dinfo,$rdev)."(G)";
		}
		elsif ($rdev_status eq 'failed') {
			$cdinfo[$k] = &rdev2disk(\@dinfo,$rdev)."(F)";
		}
		elsif ($rdev_status eq 'spare') {
			$cdinfo[$k] = &rdev2disk(\@dinfo,$rdev)."(S)";
		}
		elsif ($rdev_status eq 'removed') {
			$cdinfo[$k] = &rdev2disk(\@dinfo,$rdev)."(R)";
		}
		else {
			$cdinfo[$k] = &rdev2disk(\@dinfo,$rdev)."(U)";
		}
	}

	#print join("," , sort(@cdinfo));
        #@disks = get_unit_disks_all($r->{'name'});
	@disks = split(",", $r->{'disks'});
	foreach(@disks){
		if($_ =~ /\d+/){
			#$_ = get_disk_id($_, @dinfo);
		}
	}
        print join("," , sort(@disks)); 
	print "</td>\n";

	print "</tr>\n";
}
print "</table> </td>\n";

#if has usr vol, then print (detail, recovery, edit, delete) button
if ($validrd) {
	print "<td valign=top width=60>",
    	"<table class=mode1 border=0 cellspacing=8 cellpadding=1 width=6%>\n";
	if ($mkfs_lock) {
	print "<tr><td align=center><input type=submit name=vdetail value=\"$text{'detail'}\" onclick='return SetEditValue(this,$i)' class=button></td></tr>\n";
	print "<tr><td align=center><input type=submit name=vrecovery disabled=true value=\"$text{'vrecovery'}\" onclick='return SetEditValue(this,$i)' class=button></td></tr>\n";
	print "<tr><td align=center><input type=submit name=vdelete disabled=true value=\"$text{'delete'}\" onclick='return SetEditValue(this,$i)' class=button></td></tr>\n";
	}
	else {
	print "<tr><td align=center><input type=hidden name=vdetail value=\"$text{'detail'}\" onclick='return SetEditValue(this,$i)' class=button></td></tr>\n";
	print "<tr><td align=center><input type=submit name=vrecovery disabled=true value=\"$text{'vrecovery'}\" onclick='return SetEditValue(this,$i)' class=button></td></tr>\n";
	print "<tr><td align=center><input type=submit name=vdelete value=\"$text{'delete'}\" onclick='return SetEditValue(this,$i)' class=button></td></tr>\n";
	print "<tr><td><input type=hidden name=mig value=\"$text{'bt_migrate'}\" onclick='return SetEditValue(this,$i)' class=button disabled=true></td></tr>\n";
	}
	print "</table></td>\n";
        print "<tr><td><i> $text{'index_disk_explain2'} </i></td></tr>\n\n";
        print "<tr height=5><td> </td></tr>\n";
	print "<tr> <td><i>  $text{'index_disk_explain'} </i> </td> </tr>\n";
}


print "</tr>\n";

#create disk region
print "<tr height=20> <td> </td> </tr>\n";
if ($freedisk) {
	if ($mkfs_lock) {
		print "<tr><td align=left><input type=submit name=vcreate disabled=true value=\"$text{'create_vol'}\" onclick='return SetEditValue(this,$j)' class=button></td></tr>\n";
	}
	else {
		print "<tr><td align=left><input type=submit name=vcreate value=\"$text{'create_vol'}\" onclick='return SetEditValue(this,$j)' class=button></td></tr>\n";
	}

}
else {
	print "<tr><td> <i> $text{'nofreedisk'} </i></td></tr>\n";
}

if ($mkfs_lock == 1) {
	print "<tr height=20> <td> </td> </tr>\n";
	print "<tr><td> <i> $text{'fscklock'} </i></td></tr>\n";
}
elsif ($mkfs_lock == 2) {
	print "<tr height=20> <td> </td> </tr>\n";
	print "<tr><td> <i> $text{'noopdisk'} </i></td></tr>\n";
}

print "</table> </td>\n";

print "<input type=hidden name=diskindex>\n";
print "<input type=hidden name=volname>\n";
print "<input type=hidden name=lvname>\n";
#java script
print <<EOF;
<script>
function onsubmit_form(vcreate_exist, vdetail_exist, vrecovery_exist, vdelete_exist)
{
        if (vcreate_exist == 1) {
	    document.editform.vcreate.disabled = true;
        }
        
        if (vdetail_exist == 1) {
            document.editform.vdetail.disabled = true;
        }
        
        if (vrecovery_exist == 1) {
            document.editform.vrecovery.disabled = true;
        }
        
        if (vdelete_exist == 1) {
            document.editform.vdelete.disabled = true;
        }
        document.editform.dformat.disabled = true;
}

function GetSelected_volume(obj, n)
{
	var i;
	var found = false;
	if (n > 1) {
		for (i = 0; i < document.editform.volume.length; i++) {
			if (document.editform.volume[i].checked) {
				found = true;
				break;
			}
		}
	}
	else if (n == 1) {
		if (document.editform.volume.checked) { 
			found = true;
		}
	}

	if (found) {
		if (n > 1)
			document.editform.volname.value = document.editform.volume[i].value;
		else
			document.editform.volname.value = document.editform.volume.value;
	}
	else
		document.editform.volname.value = '';

	if (obj == document.editform.vcreate)
		found = true;

	return found;
}

function SetEditValue(obj, n)
{
	if (GetSelected_volume(obj, n))	{
		if (obj == document.editform.vdelete) {
			if (!confirm("$text{'delete_volume_query'}"))
				return false;
		} 
	}
	else {  
		alert("$text{'noselectvol'}");
		return false;
	}
	if (obj == document.editform.vcreate) {
		document.editform.action = 'vol_create.cgi';
	}
	else if (obj == document.editform.vdelete) {
		document.editform.action = 'vol_delete.cgi';
	}
	else if (obj == document.editform.vrecovery) {
		document.editform.action = 'vol_recovery.cgi';
	}
        else if (obj == document.editform.filesystem) {
                document.editform.action = 'vol_detail.cgi';
        }
	else if (obj == document.editform.mig){
		document.editform.action = 'vol_migrate.cgi';
	}
	else {
		document.editform.action = 'vol_detail.cgi';
	}

	return true;
}


function SetRecoveryStatus(CanRecovery, CanMigrate)
{
	if (CanRecovery == 1) {
		document.editform.vrecovery.disabled = false;
	}
	else
	{
		document.editform.vrecovery.disabled = true;
	}

        if (CanMigrate == 1) {
                document.editform.mig.disabled = false;
        }
        else
        {
                document.editform.mig.disabled = true;
        }
}

function checkstatus(status, dvolname)
{
	if (status == 'well') {
		document.editform.dformat.disabled  = false;
		document.editform.dhtswap.disabled  = false;
	} 
	else if (status == 'unknown') {
		document.editform.dformat.disabled  = false;
		document.editform.dhtswap.disabled  = false;
	}
	else if (status == 'offline') {
		document.editform.dformat.disabled  = false;
		document.editform.dhtswap.disabled  = false;
	}
	else if (status == 'failed') {
		document.editform.dformat.disabled  = false;
		document.editform.dhtswap.disabled  = true;
	}
        else if ((status == 'Online') || (status == 'DEGRADED')){
                document.editform.dformat.disabled  = false;
                document.editform.dhtswap.disabled  = false;
             if (dvolname == ''){
                document.editform.dspare.disabled = false;
             }
             else if(dvolname == 'spare') {
                document.editform.dspare.disabled = false;
             }
             else {
                document.editform.dspare.disabled = true;
             }
        }
        else if (status == 'NOT-PRESENT') {
                document.editform.dformat.disabled  = false;
                document.editform.dhtswap.disabled  = true;
                document.editform.dspare.disabled = true;
        }
}

function SetEditValue_disk(obj, n)
{
	if (GetSelected_disk(obj, n))	{

                if (obj == document.editform.dhtswap) {
                        if (!confirm("Are you sure removing this disk?"))
                                return fals ;
                }
	}
	else {
                if(obj != document.editform.dformat){
		    alert("$text{'noselectdisk'}");
		    return false;
                }
	}
	if (obj == document.editform.dformat) {
		document.editform.action = '3w_disk_rescan.cgi';
	}
	else if (obj == document.editform.dhtswap) {
//		document.editform.action = 'diskswap.cgi';
                document.editform.action = '3w_diskswap.cgi';
	}
        else if (obj == document.editform.dspare) {
                document.editform.action = 'spare.cgi';
        }
	return true;
}

function GetSelected_disk(obj, n)
{
	var i;
	var found = false;

	if (n > 1) {
		for (i = 0; i < document.editform.disk.length; i++) {
			if (document.editform.disk[i].checked) {
				found = true;
				break;
			}
		}
	}
	else if (n == 1) {
		if (document.editform.disk.checked) {
			found = true;
		}
	}

	if (found) {
		if (n > 1)
			document.editform.disk.value = document.editform.disk[i].value;
		else
			document.editform.disk.value = document.editform.disk.value;
	}
	else
		document.editform.disk.value = '';

	return found;
}

</script>

EOF

print "</form>\n";
&footer("/", $text{'index'});

